package flown.game;


import flown.math.Rect;
import flown.window.graphics.Renderer2D;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


/**
 * @author flown
 */


/*
 * 图像读取
 * 1.Image image = ImageIO.read(new FileInputStream(“文件路径”));
 * 2.Image image = ImageIO.read(new File（）);
 * 3.Image image = ImageIO.read(new URL（）));
 *
 * 4.Toolkit.getDefaultToolkit().getImage(“图片路径”)；
 *
 * 5.new ImageIcon("图片路径).getImage()；
 */


public class Sprite extends GameObject
{
    private Image image;
    private ArrayList<Rect> source_rect = null;///< source rect。源矩形，画的位置为目标，裁剪的位置为源位置。 图像上所要绘制图像的矩形区域，默认为整个图像。

    public Sprite(String image_path) throws IOException { this(0,0,0,0,image_path); }
    public Sprite(Image image) throws IOException { this(0,0,0,0,image); }
    public Sprite(int x, int y, int width, int height, String image_path) throws IOException
    {
        super(x, y, width, height);
        this.image = ImageIO.read(new File(image_path));
    }

    public Sprite(int x, int y, int width, int height, Image image)
    {
        super(x, y, width, height);
        this.image = image;
    }

    /**
     * 绘制精灵
     * @param renderer Renderer2D
     */
    public final void Render(Renderer2D renderer)
    {
        renderer.DrawImage
                (
                        this.image,
                        this.rect.x, this.rect.y,
                        this.rect.width, this.rect.height,
                        null
                );
    }

    /**
     * 绘制精灵
     * @param renderer Renderer2D
     * @param source_rect_index source rect index
     */
    public final void Render(Renderer2D renderer,int source_rect_index)
    {
        Rect sr = this.source_rect.get(source_rect_index);
        if (sr != null)
        {
            renderer.DrawImage
                    (
                            this.image,
                            (float)this.rect.x, (float)this.rect.y,
                            this.rect.width, this.rect.height,
                            sr.x, sr.y,
                            sr.width, sr.height,
                            null
                    );
        }
        else
        {
            renderer.DrawImage
                    (
                            this.image,
                            this.rect.x, this.rect.y,
                            this.rect.width, this.rect.height,
                            null
                    );
        }
    }


    /**
     * 初始化source rect
     * @param x rect开始x
     * @param y rect开始y
     * @param width rect_width
     * @param height rect_height
     * @param horizontal_n 水平数量
     * @param vertical_n 垂直数量
     */
    public final void InitRect(int x, int y, int width, int height,
                         int horizontal_n, int vertical_n)
    {
        this.source_rect = new ArrayList<Rect>();
        for (int vertical_count = 0;vertical_count < vertical_n;vertical_count++)
        {
            for (int horizontal_count = 0;horizontal_count < horizontal_n;horizontal_count++)
            {
                this.source_rect.add(
                        new Rect(
                                x + width * horizontal_count,
                                y + height * vertical_count,
                                width,
                                height
                        )
                );
            }
        }
    }

}

















